<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2019/8/6
 * Time: 1:43
 */

namespace app\admin\controller;



use app\admin\model\ApprovalDepartment;
use app\admin\model\ApprovalLeave;
use app\admin\model\ApprovalPurchase;
use app\admin\model\ApprovalSeal;
use app\admin\model\ApprovalOvertime;

class Approval extends Admin
{
    /**
     * 在线审批
     */
    public function leave() {
        $query = request()->param();
        $status = input('status','all');
        $start_time = input('start_time');
        $end_time = input('end_time');
        $list = $this->getDifferentDetail(1,1,$start_time,$end_time,$status,$query);
        $this->assign('list',$list);
        $msg = [
            'start_time' => $start_time,
            'end_time' => $end_time,
            'status' => $status
        ];
        $this->assign('msg',$msg);
        return $this->fetch();
    }

    /**
     * 导出
     */
    public function export1() {
        //引入Excel类
        $objPHPExcel = new \PHPExcel();
        // 设置excel文档的属性
        $objPHPExcel->getProperties()->setCreator("cyf")
            ->setLastModifiedBy("cyf Test")
            ->setTitle("order")
            ->setSubject("Test1")
            ->setDescription("Test2")
            ->setKeywords("Test3")
            ->setCategory("Test result file");
        //设置excel工作表名及文件名
        $title = '请假审批记录表';
        $excel_filename = '请假审批记录表_'.date('YmdHis');
        // 操作第一个工作表
        $objPHPExcel->setActiveSheetIndex(0);
        //第一行设置内容
        $objPHPExcel->getActiveSheet()->setCellValue('A1',$excel_filename);
        //合并
        $objPHPExcel->getActiveSheet()->mergeCells('A1:G1');
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //设置单元格内容加粗
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        //设置单元格内容水平居中
        $objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //设置excel的表头
        $sheet_title = array('申请人','请假类型','请假时间','请假天数','申请时间','内容备注','审批状态');
        // 设置第一行和第一行的行高
        $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);
        $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(25);
        $letter = array('A','B','C','D','E','F','G');
        //设置单元格
        $objPHPExcel->getActiveSheet()->getStyle('A2:G2')->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
        //首先是赋值表头
        for ($k=0;$k<7;$k++) {
            $objPHPExcel->getActiveSheet()->setCellValue($letter[$k].'2',$sheet_title[$k]);
            $objPHPExcel->getActiveSheet()->getStyle($letter[$k].'2')->getFont()->setSize(10)->setBold(true);
            //设置单元格内容水平居中
            $objPHPExcel->getActiveSheet()->getStyle($letter[$k].'2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //设置每一列的宽度
            $objPHPExcel->getActiveSheet()->getColumnDimension($letter[$k])->setWidth(18);
            $objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(30);
        }

        $status = input('status','all');
        $start_time = input('start_time');
        $end_time = input('end_time');
        $list = $this->getDifferentDetail(1,2,$start_time,$end_time,$status);
        $count = count($list);
        //开始赋值
        for ($i=0;$i<$count;$i++) {
            //先确定行
            $row = $i+3;//再确定列，最顶部占一行，表头占用一行，所以加3
            $temp = $list[$i];
            for ($j = 0; $j<7; $j++) {
                //开始为每个单元格赋值
                switch ($j) {
                    case 0 :
                        $cellvalue = $temp['apply_name'];
                        break;
                    case 1 :
                        $cellvalue = $temp['type_text'];
                        break;
                    case 2 :
                        $cellvalue = $temp['start_time_text'].' - '.$temp['end_time_text'];
                        break;
                    case 3 :
                        $cellvalue = $temp['duration'].'天';
                        break;
                    case 4 :
                        $cellvalue = $temp['apply_time_text'];
                        break;
                    case 5 :
                        $cellvalue = $temp['content'];
                        break;
                    case 6 :
                        $cellvalue = $temp['status_text'];
                        break;
                }
                //赋值
                $objPHPExcel->getActiveSheet()->setCellValue($letter[$j].$row, $cellvalue);
                //设置字体大小
                $objPHPExcel->getActiveSheet()->getStyle($letter[$j].$row)->getFont()->setSize(10);
                //设置单元格内容水平居中
                $objPHPExcel->getActiveSheet()->getStyle($letter[$j].$row)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            }
            // 设置行高
            $objPHPExcel->getActiveSheet()->getRowDimension($row)->setRowHeight(21);
        }
        unset($list);

        //赋值结束，开始输出
        $objPHPExcel->getActiveSheet()->setTitle($title);
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$excel_filename.'.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //生成xlsx文件并存入当前文件目录
        function saveExcelToLocalFile($objWriter,$excel_filename)
        {
            // make sure you have permission to write to directory
            $filePath = './uploads/excel/' . $excel_filename . '.xls';
            $objWriter->save($filePath);
            return $filePath;
        }
        $response = [
            'code' => 0,
            'url' => saveExcelToLocalFile($objWriter,$excel_filename),
        ];
        return json_encode($response);
//        dump($objWriter);
//        $objWriter->save('php://output');
    }

    /**
     * 公章使用
     */
    public function seal() {
        $query = request()->param();
        $status = input('status','all');
        $start_time = input('start_time');
        $end_time = input('end_time');
        $list = $this->getDifferentDetail(2,1,$start_time,$end_time,$status,$query);
        $this->assign('list',$list);
        $msg = [
            'start_time' => $start_time,
            'end_time' => $end_time,
            'status' => $status
        ];
        $this->assign('msg',$msg);
        return $this->fetch();
    }

    /**
     * 导出
     */
    public function export2() {
        //引入Excel类
        $objPHPExcel = new \PHPExcel();
        // 设置excel文档的属性
        $objPHPExcel->getProperties()->setCreator("cyf")
            ->setLastModifiedBy("cyf Test")
            ->setTitle("order")
            ->setSubject("Test1")
            ->setDescription("Test2")
            ->setKeywords("Test3")
            ->setCategory("Test result file");
        //设置excel工作表名及文件名
        $title = '公章使用审批记录表';
        $excel_filename = '公章使用审批记录表_'.date('YmdHis');
        // 操作第一个工作表
        $objPHPExcel->setActiveSheetIndex(0);
        //第一行设置内容
        $objPHPExcel->getActiveSheet()->setCellValue('A1',$excel_filename);
        //合并
        $objPHPExcel->getActiveSheet()->mergeCells('A1:F1');
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //设置单元格内容加粗
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        //设置单元格内容水平居中
        $objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //设置excel的表头
        $sheet_title = array('申请人','申请部门','发往部门','申请时间','内容备注','审批状态');
        // 设置第一行和第一行的行高
        $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);
        $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(25);
        $letter = array('A','B','C','D','E','F');
        //设置单元格
        $objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
        //首先是赋值表头
        for ($k=0;$k<6;$k++) {
            $objPHPExcel->getActiveSheet()->setCellValue($letter[$k].'2',$sheet_title[$k]);
            $objPHPExcel->getActiveSheet()->getStyle($letter[$k].'2')->getFont()->setSize(10)->setBold(true);
            //设置单元格内容水平居中
            $objPHPExcel->getActiveSheet()->getStyle($letter[$k].'2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //设置每一列的宽度
            $objPHPExcel->getActiveSheet()->getColumnDimension($letter[$k])->setWidth(18);
            $objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(30);
        }

        $status = input('status','all');
        $start_time = input('start_time');
        $end_time = input('end_time');
        $list = $this->getDifferentDetail(2,2,$start_time,$end_time,$status);
        $count = count($list);
        //开始赋值
        for ($i=0;$i<$count;$i++) {
            //先确定行
            $row = $i+3;//再确定列，最顶部占一行，表头占用一行，所以加3
            $temp = $list[$i];
            for ($j = 0; $j<6; $j++) {
                //开始为每个单元格赋值
                switch ($j) {
                    case 0 :
                        $cellvalue = $temp['apply_name'];
                        break;
                    case 1 :
                        $cellvalue = $temp['apply_department_name'];
                        break;
                    case 2 :
                        $cellvalue = $temp['to_company'];
                        break;
                    case 3 :
                        $cellvalue = $temp['apply_time_text'];
                        break;
                    case 4 :
                        $cellvalue = $temp['content'];
                        break;
                    case 5 :
                        $cellvalue = $temp['status_text'];
                        break;
                }
                //赋值
                $objPHPExcel->getActiveSheet()->setCellValue($letter[$j].$row, $cellvalue);
                //设置字体大小
                $objPHPExcel->getActiveSheet()->getStyle($letter[$j].$row)->getFont()->setSize(10);
                //设置单元格内容水平居中
                $objPHPExcel->getActiveSheet()->getStyle($letter[$j].$row)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            }
            // 设置行高
            $objPHPExcel->getActiveSheet()->getRowDimension($row)->setRowHeight(21);
        }
        unset($list);

        //赋值结束，开始输出
        $objPHPExcel->getActiveSheet()->setTitle($title);
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$excel_filename.'.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //生成xlsx文件并存入当前文件目录
        function saveExcelToLocalFile($objWriter,$excel_filename)
        {
            // make sure you have permission to write to directory
            $filePath = './uploads/excel/' . $excel_filename . '.xls';
            $objWriter->save($filePath);
            return $filePath;
        }
        $response = [
            'code' => 0,
            'url' => saveExcelToLocalFile($objWriter,$excel_filename),
        ];
        return json_encode($response);
//        dump($objWriter);
//        $objWriter->save('php://output');
    }

    /**
     * 物资采购
     */
    public function purchase() {
        $query = request()->param();
        $status = input('status','all');
        $start_time = input('start_time');
        $end_time = input('end_time');
        $list = $this->getDifferentDetail(3,1,$start_time,$end_time,$status,$query);
        $this->assign('list',$list);
        $msg = [
            'start_time' => $start_time,
            'end_time' => $end_time,
            'status' => $status
        ];
        $this->assign('msg',$msg);
        return $this->fetch();
    }

    /**
     * 导出
     */
    public function export3() {
        //引入Excel类
        $objPHPExcel = new \PHPExcel();
        // 设置excel文档的属性
        $objPHPExcel->getProperties()->setCreator("cyf")
            ->setLastModifiedBy("cyf Test")
            ->setTitle("order")
            ->setSubject("Test1")
            ->setDescription("Test2")
            ->setKeywords("Test3")
            ->setCategory("Test result file");
        //设置excel工作表名及文件名
        $title = '物资采购（内购）记录表';
        $excel_filename = '物资采购（内购）记录表_'.date('YmdHis');
        // 操作第一个工作表
        $objPHPExcel->setActiveSheetIndex(0);
        //第一行设置内容
        $objPHPExcel->getActiveSheet()->setCellValue('A1',$excel_filename);
        //合并
        $objPHPExcel->getActiveSheet()->mergeCells('A1:F1');
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //设置单元格内容加粗
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        //设置单元格内容水平居中
        $objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //设置excel的表头
        $sheet_title = array('申请人','申请部门','申请时间','状态','','');
        // 设置第一行和第一行的行高
        $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);
        $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(25);
        $letter = array('A','B','C','D','E','F');
        //设置单元格
        $objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
        //首先是赋值表头
        for ($k=0;$k<6;$k++) {
            $objPHPExcel->getActiveSheet()->setCellValue($letter[$k].'2',$sheet_title[$k]);
            $objPHPExcel->getActiveSheet()->getStyle($letter[$k].'2')->getFont()->setSize(10)->setBold(true);
            //设置单元格内容水平居中
            $objPHPExcel->getActiveSheet()->getStyle($letter[$k].'2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //设置每一列的宽度
            $objPHPExcel->getActiveSheet()->getColumnDimension($letter[$k])->setWidth(18);
            $objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(30);
        }
        $status = input('status','all');
        $start_time = input('start_time');
        $end_time = input('end_time');
        $list = $this->getDifferentDetail(3,2,$start_time,$end_time,$status);
        $count = count($list);
        //开始赋值
        for ($i=0;$i<$count;$i++) {
            //先确定行
            $row = $i+3;//再确定列，最顶部占一行，表头占用一行，所以加3
            $temp = $list[$i];
            for ($j = 0; $j<6; $j++) {
                if($temp['type'] == 1) {
                    //开始为每个单元格赋值
                    switch ($j) {
                        case 0 :
                            $cellvalue = $temp['apply_name'];
                            break;
                        case 1 :
                            $cellvalue = $temp['apply_department_name'];
                            break;
                        case 2 :
                            $cellvalue = $temp['apply_time'];
                            break;
                        case 3 :
                            $cellvalue = $temp['status_text'];
                            break;
                        case 4 :
                            $cellvalue = '';
                            break;
                        case 5 :
                            $cellvalue = '';
                            break;
                    }
                }else {
                    //开始为每个单元格赋值
                    switch ($j) {
                        case 0 :
                            $cellvalue = $temp['name'];
                            break;
                        case 1 :
                            $cellvalue = $temp['model'];
                            break;
                        case 2 :
                            $cellvalue = $temp['company'];
                            break;
                        case 3 :
                            $cellvalue = $temp['number'];
                            break;
                        case 4 :
                            $cellvalue = $temp['arrival_time_text'];
                            break;
                        case 5 :
                            $cellvalue = $temp['content'];
                            break;
                    }
                }
                //赋值
                $objPHPExcel->getActiveSheet()->setCellValue($letter[$j].$row, $cellvalue);
                //设置字体大小
                $objPHPExcel->getActiveSheet()->getStyle($letter[$j].$row)->getFont()->setSize(10);
                if($temp['type'] == 1) {
                    //设置单元格背景
                    $objPHPExcel->getActiveSheet()->getStyle( $letter[$j].$row)->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
                    $objPHPExcel->getActiveSheet()->getStyle( $letter[$j].$row)->getFill()->getStartColor()->setARGB('FFdcdcdc');
                }
                if($temp['type'] == 2) {
                    //字体加粗
                    $objPHPExcel->getActiveSheet()->getStyle($letter[$j].$row)->getFont()->setBold(true);
                }
                //设置单元格内容水平居中
                $objPHPExcel->getActiveSheet()->getStyle($letter[$j].$row)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            }


            // 设置行高
            $objPHPExcel->getActiveSheet()->getRowDimension($row)->setRowHeight(21);
        }
        unset($list);

        //赋值结束，开始输出
        $objPHPExcel->getActiveSheet()->setTitle($title);
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$excel_filename.'.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //生成xlsx文件并存入当前文件目录
        function saveExcelToLocalFile($objWriter,$excel_filename)
        {
            // make sure you have permission to write to directory
            $filePath = './uploads/excel/' . $excel_filename . '.xls';
            $objWriter->save($filePath);
            return $filePath;
        }
        $response = [
            'code' => 0,
            'url' => saveExcelToLocalFile($objWriter,$excel_filename),
        ];
        return json_encode($response);
//        dump($objWriter);
//        $objWriter->save('php://output');
    }

    /**
     * 部门联系单
     */
    public function department() {
        $query = request()->param();
        $status = input('status','all');
        $start_time = input('start_time');
        $end_time = input('end_time');
        $list = $this->getDifferentDetail(4,1,$start_time,$end_time,$status,$query);
        $this->assign('list',$list);
        $msg = [
            'start_time' => $start_time,
            'end_time' => $end_time,
            'status' => $status
        ];
        $this->assign('msg',$msg);
        return $this->fetch();
    }

    /**
     * 导出
     */
    public function export4() {
        //引入Excel类
        $objPHPExcel = new \PHPExcel();
        // 设置excel文档的属性
        $objPHPExcel->getProperties()->setCreator("cyf")
            ->setLastModifiedBy("cyf Test")
            ->setTitle("order")
            ->setSubject("Test1")
            ->setDescription("Test2")
            ->setKeywords("Test3")
            ->setCategory("Test result file");
        //设置excel工作表名及文件名
        $title = '部门联系单记录表';
        $excel_filename = '部门联系单记录表_'.date('YmdHis');
        // 操作第一个工作表
        $objPHPExcel->setActiveSheetIndex(0);
        //第一行设置内容
        $objPHPExcel->getActiveSheet()->setCellValue('A1',$excel_filename);
        //合并
        $objPHPExcel->getActiveSheet()->mergeCells('A1:G1');
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //设置单元格内容加粗
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        //设置单元格内容水平居中
        $objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //设置excel的表头
        $sheet_title = array('申请人','交办部门','办理部门','交办时间','计划完成时间','申请时间','审批状态');
        // 设置第一行和第一行的行高
        $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);
        $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(25);
        $letter = array('A','B','C','D','E','F','G');
        //设置单元格
        $objPHPExcel->getActiveSheet()->getStyle('A2:G2')->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
        //首先是赋值表头
        for ($k=0;$k<7;$k++) {
            $objPHPExcel->getActiveSheet()->setCellValue($letter[$k].'2',$sheet_title[$k]);
            $objPHPExcel->getActiveSheet()->getStyle($letter[$k].'2')->getFont()->setSize(10)->setBold(true);
            //设置单元格内容水平居中
            $objPHPExcel->getActiveSheet()->getStyle($letter[$k].'2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //设置每一列的宽度
            $objPHPExcel->getActiveSheet()->getColumnDimension($letter[$k])->setWidth(18);
            $objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(30);
        }

        $status = input('status','all');
        $start_time = input('start_time');
        $end_time = input('end_time');
        $list = $this->getDifferentDetail(4,2,$start_time,$end_time,$status);
        $count = count($list);
        //开始赋值
        for ($i=0;$i<$count;$i++) {
            //先确定行
            $row = $i+3;//再确定列，最顶部占一行，表头占用一行，所以加3
            $temp = $list[$i];
            for ($j = 0; $j<7; $j++) {
                //开始为每个单元格赋值
                switch ($j) {
                    case 0 :
                        $cellvalue = $temp['apply_name'];
                        break;
                    case 1 :
                        $cellvalue = $temp['assign_department_name'];
                        break;
                    case 2 :
                        $cellvalue = $temp['handle_department_name'];
                        break;
                    case 3 :
                        $cellvalue = $temp['assign_time_text'];
                        break;
                    case 4 :
                        $cellvalue = $temp['plan_time_text'];
                        break;
                    case 5 :
                        $cellvalue = $temp['apply_time_text'];
                        break;
                    case 6 :
                        $cellvalue = $temp['status_text'];
                        break;
                }
                //赋值
                $objPHPExcel->getActiveSheet()->setCellValue($letter[$j].$row, $cellvalue);
                //设置字体大小
                $objPHPExcel->getActiveSheet()->getStyle($letter[$j].$row)->getFont()->setSize(10);
                //设置单元格内容水平居中
                $objPHPExcel->getActiveSheet()->getStyle($letter[$j].$row)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            }
            // 设置行高
            $objPHPExcel->getActiveSheet()->getRowDimension($row)->setRowHeight(21);
        }
        unset($list);

        //赋值结束，开始输出
        $objPHPExcel->getActiveSheet()->setTitle($title);
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$excel_filename.'.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //生成xlsx文件并存入当前文件目录
        function saveExcelToLocalFile($objWriter,$excel_filename)
        {
            // make sure you have permission to write to directory
            $filePath = './uploads/excel/' . $excel_filename . '.xls';
            $objWriter->save($filePath);
            return $filePath;
        }
        $response = [
            'code' => 0,
            'url' => saveExcelToLocalFile($objWriter,$excel_filename),
        ];
        return json_encode($response);
//        dump($objWriter);
//        $objWriter->save('php://output');
    }

    public function overtime() {
        $query = request()->param();
        $status = input('status','all');
        $start_time = input('start_time');
        $end_time = input('end_time');
        $list = $this->getDifferentDetail(5,1,$start_time,$end_time,$status,$query);
        $this->assign('list',$list);
        $msg = [
            'start_time' => $start_time,
            'end_time' => $end_time,
            'status' => $status
        ];
        $this->assign('msg',$msg);
        return $this->fetch();
    }

    /**
     * 导出
     */
    public function export5() {
        //引入Excel类
        $objPHPExcel = new \PHPExcel();
        // 设置excel文档的属性
        $objPHPExcel->getProperties()->setCreator("cyf")
            ->setLastModifiedBy("cyf Test")
            ->setTitle("order")
            ->setSubject("Test1")
            ->setDescription("Test2")
            ->setKeywords("Test3")
            ->setCategory("Test result file");
        //设置excel工作表名及文件名
        $title = '加班审批记录表';
        $excel_filename = '加班审批记录表_'.date('YmdHis');
        // 操作第一个工作表
        $objPHPExcel->setActiveSheetIndex(0);
        //第一行设置内容
        $objPHPExcel->getActiveSheet()->setCellValue('A1',$excel_filename);
        //合并
        $objPHPExcel->getActiveSheet()->mergeCells('A1:G1');
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //设置单元格内容加粗
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        //设置单元格内容水平居中
        $objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //设置excel的表头
        $sheet_title = array('申请人','审批类型','加班时间','加班天数','申请时间','加班理由','审批状态');
        // 设置第一行和第一行的行高
        $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);
        $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(25);
        $letter = array('A','B','C','D','E','F','G');
        //设置单元格
        $objPHPExcel->getActiveSheet()->getStyle('A2:G2')->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
        //首先是赋值表头
        for ($k=0;$k<7;$k++) {
            $objPHPExcel->getActiveSheet()->setCellValue($letter[$k].'2',$sheet_title[$k]);
            $objPHPExcel->getActiveSheet()->getStyle($letter[$k].'2')->getFont()->setSize(10)->setBold(true);
            //设置单元格内容水平居中
            $objPHPExcel->getActiveSheet()->getStyle($letter[$k].'2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //设置每一列的宽度
            $objPHPExcel->getActiveSheet()->getColumnDimension($letter[$k])->setWidth(18);
            $objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(30);
        }

        $status = input('status','all');
        $start_time = input('start_time');
        $end_time = input('end_time');
        $list = $this->getDifferentDetail(5,2,$start_time,$end_time,$status);
        $count = count($list);
        //开始赋值
        for ($i=0;$i<$count;$i++) {
            //先确定行
            $row = $i+3;//再确定列，最顶部占一行，表头占用一行，所以加3
            $temp = $list[$i];
            for ($j = 0; $j<7; $j++) {
                //开始为每个单元格赋值
                switch ($j) {
                    case 0 :
                        $cellvalue = $temp['apply_name'];
                        break;
                    case 1 :
                        $cellvalue = $temp['type_text'];
                        break;
                    case 2 :
                        $cellvalue = $temp['start_time_text'].' - '.$temp['end_time_text'];
                        break;
                    case 3 :
                        $cellvalue = $temp['duration'].'天';
                        break;
                    case 4 :
                        $cellvalue = $temp['apply_time_text'];
                        break;
                    case 5 :
                        $cellvalue = $temp['content'];
                        break;
                    case 6 :
                        $cellvalue = $temp['status_text'];
                        break;
                }
                //赋值
                $objPHPExcel->getActiveSheet()->setCellValue($letter[$j].$row, $cellvalue);
                //设置字体大小
                $objPHPExcel->getActiveSheet()->getStyle($letter[$j].$row)->getFont()->setSize(10);
                //设置单元格内容水平居中
                $objPHPExcel->getActiveSheet()->getStyle($letter[$j].$row)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            }
            // 设置行高
            $objPHPExcel->getActiveSheet()->getRowDimension($row)->setRowHeight(21);
        }
        unset($list);

        //赋值结束，开始输出
        $objPHPExcel->getActiveSheet()->setTitle($title);
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$excel_filename.'.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //生成xlsx文件并存入当前文件目录
        function saveExcelToLocalFile($objWriter,$excel_filename)
        {
            // make sure you have permission to write to directory
            $filePath = './uploads/excel/' . $excel_filename . '.xls';
            $objWriter->save($filePath);
            return $filePath;
        }
        $response = [
            'code' => 0,
            'url' => saveExcelToLocalFile($objWriter,$excel_filename),
        ];
        return json_encode($response);
//        dump($objWriter);
//        $objWriter->save('php://output');
    }

    /**
     * @param $class 审批类别，1请假，2公章，3采购，4部门，5加班
     * @param $type 获取类型，1查询，2导出
     * @param string $start_time 查询开始时间
     * @param string $end_time 查询结束时间
     * @param string $status 状态：all全部，0待审批，1已审批，2不通过，3进行中
     * @param array $query 查询条件带入分页
     * @return array|false|\PDOStatement|string|\think\Collection|\think\paginator\Collection|void
     */
    private function getDifferentDetail($class,$type,$start_time = '',$end_time = '',$status = 'all',$query = []) {
        switch ($class) {
            case 1:
                $Model = new ApprovalLeave();
                break;
            case 2:
                $Model = new ApprovalSeal();
                break;
            case 3:
                $Model = new ApprovalPurchase();
                break;
            case 4:
                $Model = new ApprovalDepartment();
                break;
            case 5:
                $Model = new ApprovalOvertime();
                break;
            default:
                return $this->error("无对应类别数据，请重新检查！");
                break;
        }
        if($status == 'all') {
            $map = array(
                'status' => array('egt',0),
            );
        }else {
            $map = array(
                'status' => $status,
            );
        }
        if($start_time != '' && $end_time != '') {
            $map['apply_time'] = ['between',[strtotime($start_time." 00:00:00"),strtotime($end_time." 23:59:59")]];
        }else if($start_time != '') {
            $map['apply_time'] = ['egt',strtotime($start_time." 00:00:00")];
        }else if($end_time != '') {
            $map['apply_time'] = ['elt',strtotime($end_time." 23:59:59")];
        }
        $list = $Model->getList($type,$map,$query);

        return $list;
    }
}